import { inject, injectable } from 'inversify';
import { APIClient, BaseDTO, HttpMethod } from '@todo/core';
import {
  requestUserRolesDTO,
  requestWOMeta,
  responseCreateUpdateRole,
  responsePermissionList,
  responseUserRoleByID,
  responseUserRolesListDTO,
} from './user.roles.dto.ts';
import { EUserEndpoints } from '../config/endpoints.ts';

@injectable()
export class UsersRolesRepository {
  constructor(
    @inject(APIClient)
    private apiClient: APIClient,
  ) {}

  /*
   * Получить список ролей
   * @param params
   */
  async getRolesList(
    params: requestUserRolesDTO,
  ): Promise<responseUserRolesListDTO> {
    return this.apiClient.request<
      requestUserRolesDTO,
      responseUserRolesListDTO
    >({
      route: EUserEndpoints.GET_ROLE_LIST,
      method: HttpMethod.GET,
      requestObj: params,
    });
  }

  /**
   * Получить роль по id
   * @param _id
   */
  async getRoleById(_id: string): Promise<responseUserRoleByID> {
    return this.apiClient.request<null, responseUserRoleByID>({
      route: EUserEndpoints.GET_ROLE_BY_ID + `/${_id}`,
      method: HttpMethod.GET,
    });
  }

  /**
   * Получить список всех возможных разрешений
   */
  async getPermissionList(): Promise<responsePermissionList> {
    return this.apiClient.request<null, responsePermissionList>({
      route: EUserEndpoints.GET_PERMISSION_LIST,
      method: HttpMethod.GET,
    });
  }

  /**
   * Создать роль
   */
  async createRole(params: requestWOMeta): Promise<responseCreateUpdateRole> {
    return this.apiClient.request<requestWOMeta, responseCreateUpdateRole>({
      route: EUserEndpoints.CREATE_ROLE,
      method: HttpMethod.POST,
      requestObj: params,
    });
  }

  /**
   * Обновить роль
   */
  async updateRole(
    _id: string,
    params: requestWOMeta,
  ): Promise<responseCreateUpdateRole> {
    return this.apiClient.request<requestWOMeta, responseCreateUpdateRole>({
      route: EUserEndpoints.UPDATE_ROLE + `/${_id}`,
      method: HttpMethod.PUT,
      requestObj: params,
    });
  }

  /**
   * Сделать роль неактивной
   */
  async setRoleInactive(_id: string): Promise<BaseDTO> {
    return this.apiClient.request<null, BaseDTO>({
      route: EUserEndpoints.SET_ROLE_INACTIVE + `/${_id}`,
      method: HttpMethod.PATCH,
    });
  }

  /**
   * Сделать роль активной
   */
  async setRoleActive(_id: string): Promise<BaseDTO> {
    return this.apiClient.request<null, BaseDTO>({
      route: EUserEndpoints.SET_ROLE_ACTIVE + `/${_id}`,
      method: HttpMethod.PATCH,
    });
  }

  /**
   * Удалить роль
   */
  async deleteRole(_id: string): Promise<BaseDTO> {
    return this.apiClient.request<null, BaseDTO>({
      route: EUserEndpoints.DELETE_ROLE + `/${_id}`,
      method: HttpMethod.DELETE,
    });
  }
}
